package com.sweet.imserver;

import com.sweet.imserver.db.DBVerticle;
import com.sweet.imserver.model.RespData;
import com.sweet.imserver.server.HttpIMServer;
import com.sweet.imserver.server.IMServer;
import io.vertx.core.*;
import io.vertx.core.json.JsonObject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Arrays;
import java.util.UUID;

public class MainVerticle extends AbstractVerticle {
  public static void main(String[] args) {
    Vertx.vertx().deployVerticle(new MainVerticle());
  }

  private final Logger logger = LogManager.getLogger(MainVerticle.class);

  @Override
  public void start() throws Exception {

    deploy(DBVerticle.class, new DeploymentOptions().setWorker(true))
      .compose(f -> deploy(HttpIMServer.class, new DeploymentOptions()))
      .compose(f -> deploy(IMServer.class, new DeploymentOptions()))
      .setHandler(f -> {
        if (f.succeeded()) {
          logger.debug("deploy verticle ok!");
        } else {
          logger.error("deploy error", f.cause());
        }
      });
  }

  private Future<String> deploy(Class<? extends Verticle> c, DeploymentOptions options) {
    Promise<String> promise = Promise.promise();
    vertx.deployVerticle(c, options, promise);
    logger.debug("deploy => " + c.getSimpleName());
    return promise.future();
  }
}
